import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../components/login/login.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/login',
        name: 'login',
        component: LoginView
    },
    {
        path: '/home',
        name: 'home',
        component: () =>
            import ('../views/Home.vue'),
        children: [{
                path: 'attendance',
                name: 'attendance',
                component: () =>
                    import ('../components/attendance/attendance.vue')

            },
            {
                path: 'becomeregular',
                name: 'becomeregular',
                component: () =>
                    import ('../components/becomeRegular/BecomeRegular.vue')

            },
            {
                path: 'enterpost',
                name: 'enterpost',
                component: () =>
                    import ('../components/enterPost/EnterPost.vue')

            },
            {
                path: 'leavepost',
                name: 'leavepost',
                component: () =>
                    import ('../components/leavePost/LeavePost.vue')

            },
            {
                path: 'personnellist',
                name: 'personnellist',
                component: () =>
                    import ('../components/personnelList/PersonnelList.vue')

            },
            {
                path: 'roster',
                name: 'roster',
                component: () =>
                    import ('../components/roster/RosterModle.vue')

            },
            {
                path: 'wages',
                name: 'wages',
                component: () =>
                    import ('../components/wages/WagesModle.vue')

            },
        ]
    },

    {
        path: '/',
        redirect: '/login',
    }
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    // }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
        // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
        // 判断是否登陆成功过
    if (!tokenStr) return next('/login')
    if (to.path === '/home') return next({ name: 'enterpost' })
    next()
})

export default router